<?php

class Tournament extends ElggObject
{
	protected $organizationRelationId = "tournament";
	
	protected $org;
	
	protected function initializeAttributes()
	{
		parent::initializeAttributes();
		$this -> attributes['subtype'] = 'tournament';
	}
	/*
	protected function delete()
	{
		parent::delete("Deleted by ".elgg_get_logged_in_user_guid());
	}
	/* */
	
	/**
	 * Get the organization of this tournament
	 * @return Organization
	 */
	public function getOrganization()
	{
		elgg_load_library('elgg:class_organization');
		if(!$this->org)
		{
			$orgs = parent::getEntitiesFromRelationship($this->$organizationRelationId,true);
			if(count($orgs))
			{
				$this->org = $orgs[0];
			}
		}
		
		return $this->org;
	}
	
	/**
	 * Check permissions
	 */
	public function isManager($user = null)
	{
		$this->getOrganization();
		return $this->org->isManager($user);
	}
	
	/**
	 * Convert $this->timetables data in a confortable array
	 * @return array
	 */
	public function getTimetables()
	{
		$timetables = array();
		
		if(is_array($this->timetables))
		{
			foreach ($this->timetables as $timetable)
			{
				$row = array();
				$data = explode(';', $timetable);
				
				$row['date'] 	= $data[0];
				$row['start'] 	= $data[1];
				$row['end'] 	= $data[2];
				
				$timetables[] = $row;
			}
		}
		else
		{
			$data = explode(';', $this->timetable);
				
			$row['date'] 	= $data[0];
			$row['start'] 	= $data[1];
			$row['end'] 	= $data[2];
			
			$timetables[] = $row;
		}
		
		return $timetables;
	}
	
	/**
	 * Convert $this->prizes data in a confortable array
	 * @return array
	 */
	public function getPrizes()
	{
		$prizes = array();
		
		if(is_array($this->prizes))
		{
			foreach ($this->prizes as $prize)
			{
				$row = array();
				$data = explode(';', $prize);
				
				$row['condition'] 	= $data[0];
				$row['description']	= $data[1];
				
				$prizes[] = $row;
			}
		}
		else
		{
			$data = explode(';', $this->prizes);
			$row['condition'] 	= $data[0];
			$row['description']	= $data[1];
			
			$prizes[] = $row;
		}
		
		return $prizes;
	}
}
